package com.jichaoyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jichaoyun.common.enums.MyExceptiontType;

import com.jichaoyun.common.ex.MyException;
import com.jichaoyun.common.model.PagedResult;
import com.jichaoyun.dao.mapper.FaultMapper;
import com.jichaoyun.dao.mapper.FaultPictureMapper;
import com.jichaoyun.dao.mapper.LineInfoMapper;
import com.jichaoyun.model.entity.Fault;
import com.jichaoyun.model.entity.FaultPicture;
import com.jichaoyun.model.vo.FaultFormVo;
import com.jichaoyun.model.vo.FaultParaVo;
import com.jichaoyun.service.FaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
* @author zhen
* @description 针对表【fault】的数据库操作Service实现
* @createDate 2024-03-29 14:51:30
*/
@Service
public class FaultServiceImpl extends ServiceImpl<FaultMapper, Fault>
    implements FaultService {

    @Autowired
    LineInfoMapper lineInfoMapper;

    @Autowired
    FaultMapper faultMapper;

    @Resource
    FaultPictureMapper faultPictureMapper;

    /**
     * 根据一些列的条件查找出结果
     * @param faultParaVo
     * @return
     */

    @Override
    public PagedResult getMenuListPage(FaultParaVo faultParaVo) {
        Page<Fault> faultPage = new Page<>(faultParaVo.getPageNo(), 10);
        LambdaQueryWrapper<Fault> wp = new LambdaQueryWrapper<>();
        if(faultParaVo.getHandle()==1){
            wp.eq(Fault::getOperate,"待处理");
        }
        if(faultParaVo.getHandle()==2){
            wp.eq(Fault::getOperate,"已处理");
        }
        if(faultParaVo.getTimeSelect()==1){
            LocalDateTime end = LocalDateTime.now();
            LocalDateTime start = end.minusDays(7);
            wp.le(Fault::getUploadTime,end).ge(Fault::getUploadTime,start);
        }
        if(faultParaVo.getTimeSelect()==2){
            LocalDateTime end = LocalDateTime.now();
            LocalDateTime start = end.minusMonths(1);
            wp.le(Fault::getUploadTime,end).ge(Fault::getUploadTime,start);
        }
        if(faultParaVo.getTimeSelect()==3){
            LocalDateTime end = LocalDateTime.now();
            LocalDateTime start = end.minusYears(1);
            wp.le(Fault::getUploadTime,end).ge(Fault::getUploadTime,start);
        }
        if(faultParaVo.getTimeSelect()==4){
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime start = LocalDateTime.parse(faultParaVo.getTimeStart(), dateTimeFormatter);
            LocalDateTime end = LocalDateTime.parse(faultParaVo.getTimeEnd(), dateTimeFormatter);
            wp.le(Fault::getUploadTime,end).ge(Fault::getUploadTime,start);
        }
        if(faultParaVo.getLine()!=-1){
            try {
                String lineName = lineInfoMapper.selectById(faultParaVo.getLine()).getLineName();
                wp.eq(Fault::getLineName,lineName);
            }
            catch (NullPointerException e){
                throw new MyException(MyExceptiontType.NULL1,null);
            }
        }

        if (faultParaVo.getType()==1){
            wp.eq(Fault::getType,"管线故障");
        }
        if (faultParaVo.getType()==2){
            wp.eq(Fault::getType,"线路破损");
        }
        if (faultParaVo.getType()==3){
            wp.eq(Fault::getType,"其他故障");
        }
        wp.orderByDesc(Fault::getUploadTime);
        faultMapper.selectPage(faultPage,wp);
        PagedResult pagedResult = new PagedResult();

        List<Fault> records = faultPage.getRecords();
        //更新pic名称列表
        for (Fault record : records) {
            List<String> picNameList = new ArrayList<>();
            LambdaQueryWrapper<FaultPicture> wp1 = new LambdaQueryWrapper<>();
            wp1.eq(FaultPicture::getNum,record.getNum());
            List<FaultPicture> faultPictures = faultPictureMapper.selectList(wp1);
            for (FaultPicture faultPicture : faultPictures) {
                picNameList.add(faultPicture.getPicName());
            }
            record.setPicNameList(picNameList);
        }

        pagedResult.setRows(records);
        pagedResult.setPages(faultPage.getPages());
        pagedResult.setTotal(faultPage.getTotal());

        return pagedResult;
    }

    /**
     * 获取故障的通知
     * @return
     */
    @Override
    public List<FaultFormVo> getFaultInformList() {
        LambdaQueryWrapper<Fault> wp = new LambdaQueryWrapper<>();
        wp.eq(Fault::getIsRead,false).orderByDesc(Fault::getUploadTime);
        List<Fault> faults = faultMapper.selectList(wp);
        ArrayList<FaultFormVo> resList = new ArrayList<>();
        for (Fault fault : faults) {
            FaultFormVo faultFormVo = new FaultFormVo();
            faultFormVo.setStatus(fault.getStatus());
            faultFormVo.setType(fault.getType());
            faultFormVo.setSite(fault.getSite());

            LocalDateTime now = LocalDateTime.now();
            Duration between = Duration.between(fault.getUploadTime(),now);
            if(between.toHours()<1){
                faultFormVo.setTime(between.toMinutes()+"分钟前");
            }else if(between.toHours()<24){
                faultFormVo.setTime(between.toHours()+"小时前");
            }else{
                faultFormVo.setTime(between.toDays()+"天前");
            }
            resList.add(faultFormVo);
        }
        return resList;
    }
}




